<?php
namespace app\app\controller\v1;
use app\app\Appbase;

class IntegralGift extends Appbase 
{
	/**
	*获取商品分类
	* /app/v1/integral_gift/getCate
	* 需要登录 
	* @is_need_login true
	*/
	public function getCate(){
		try {
			$shopId = $this->request->userInfo['shop_id'];
			$model = model("goods")->alias('a');
    		$model = $model->where('a.status',1);
			$model = $model->where('a.is_del',0);
			$model = $model->where('a.is_apply',1);
			$model = $model->where('a.is_integral_gift',1);
			$model = $model->where('a.shop_id',$shopId);
			$cateIds =  $model->column('cate_id');
	        $cates =  model("goodsCate")->where('id','in',$cateIds)->field('id,name')->select();
			return $this->ReSucess($cates);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	*获取商品列表
	* /app/v1/integral_gift/getList
	* 需要登录 
	* @is_need_login true
	* @method get
	*/
	public function getList($cate_id='',$name=''){
		try {
			$shopId = $this->request->userInfo['shop_id'];
			$model = model("goods")->alias('a');
			$model = $model->leftJoin("GoodsSpecs c","c.goods_id = a.id and c.is_default=1");
    		$model = $model->field("
    			a.id,
	    		a.sales,
	    		a.id as goods_id,
	    		a.goods_no,
	    		a.name as goods_name,
	    		a.img as goods_img,
	    		a.banners as goods_banners,
	    		a.need_integral,
    			ifnull(c.name,'') specs_name,
	    		ifnull(c.id,'') specs_id,
	    		ifnull(c.sale_price,0) sale_price
    		");
    		$model = $model->where('a.status',1);
			$model = $model->where('a.is_del',0);
			$model = $model->where('a.is_apply',1);
			$model = $model->where('a.is_integral_gift',1);
			$model = $model->where('a.shop_id',$shopId);
			$cate_id && $model = $model->where('a.cate_id',$cate_id);
			$name && $model = $model->where('a.name','like',"%".$name."%");
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$model = $model->order('a.sales desc,a.sort_no asc');
			$result =  $model->select();
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	*获取商品明细
	* /app/v1/integral_gift/details
	* 需要登录 
	* @is_need_login false
	* @method get
	*/
	public function details($id){
		try {
			$model = model("goods")->alias('a');
			$model = $model->leftJoin("GoodsSpecs c","c.goods_id = a.id and c.is_default=1");
			$model = $model->leftJoin("GoodsSpecsStock k",'k.specs_id = c.id');
    		$model = $model->field("
    			a.id,
	    		a.sales,
	    		a.id as goods_id,
	    		a.goods_no,
	    		a.name as goods_name,
	    		a.img as goods_img,
	    		a.banners as goods_banners,
	    		a.remark as goods_remark,
	    		a.need_integral,
	    		a.give_type,
    			ifnull(c.name,'') specs_name,
	    		ifnull(c.id,'') specs_id,
	    		ifnull(c.sale_price,0) sale_price,
	    		ifnull( k.stock - k.frozen_stock,0) as stock
    		");
			$result =  $model->get($id);
			$result['goods_remark'] = htmlspecialchars_decode( $result['goods_remark'] );
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	

	
}



